package com.symria.server.bo.role;

import java.util.ArrayList;
import java.util.List;

import com.symria.framework.constant.ATTFrameWork;
import com.symria.framework.page.Page;
import com.symria.framework.util.FrameCache;
import com.symria.framework.util.StringUtil;
import com.symria.server.bo.common.CommonService;
import com.symria.server.dao.role.RoleDao;
import com.symria.server.po.AuthInfo;
import com.symria.server.po.Constant;
import com.symria.server.po.MenuInfo;
import com.symria.server.po.RoleAuth;
import com.symria.server.po.RoleInfo;
import com.symria.server.po.RoleMenu;

public class RoleService extends CommonService {
	private RoleDao roleDao;
	
	public RoleDao getRoleDao() {
		return roleDao;
	}
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	public List<Constant> queryConstantsByPcode(String pcode,String display){
		return roleDao.queryConstantsByPcode(pcode, display);
	}
	public List<Constant> queryTreeConstants(String pcode,String display){
		List<Constant> l = roleDao.queryConstantsByPcode(pcode, display);
		for(Constant c:l){
			c.setList(roleDao.queryConstantsByPcode(c.getCode(), display));
		}
		return l;
	}
	public Constant findConstantByCode(String code){
		return roleDao.findConstantByCode(code);
	}
	public void updateRoleInfo(RoleInfo role,RoleInfo newRole){
		role.setRoleName(newRole.getRoleName());
		role.setRoleDesc(newRole.getRoleDesc());
		updateObject(role);
	}
	public void updateAuthInfo(AuthInfo auth,AuthInfo newAuth){
		auth.setAuthName(newAuth.getAuthName());
		auth.setAuthCode(newAuth.getAuthCode());
		auth.setAuthRegexp(newAuth.getAuthRegexp());
		auth.setModel(ATTFrameWork.LOGIC_TRUE.equals(newAuth.getModel())?ATTFrameWork.LOGIC_TRUE:ATTFrameWork.LOGIN_FALSE);
		updateObject(auth);
	}
	public void saveAuthInfo(AuthInfo auth){
		auth.setModel(ATTFrameWork.LOGIC_TRUE.equals(auth.getModel())?ATTFrameWork.LOGIC_TRUE:ATTFrameWork.LOGIN_FALSE);
		saveObject(auth);
	}
	public void saveMenuInfo(String href,String name,Integer pid,Integer seq,String visitility,String desc){
		MenuInfo menu = new MenuInfo();
		menu.setMenuName(name);
		menu.setMenuHref(href);
		menu.setMenuDesc(desc);
		menu.setMenuSeq(seq);
		menu.setPId(pid);
		menu.setVisitility(ATTFrameWork.LOGIC_TRUE.equals(visitility)?ATTFrameWork.LOGIC_TRUE:ATTFrameWork.LOGIN_FALSE);
		saveObject(menu);
	}
	public void updateMenuInfo(MenuInfo menu,String href,String name,Integer pid,Integer seq,String visitility,String desc){
		menu.setMenuName(name);
		menu.setMenuHref(href);
		menu.setMenuDesc(desc);
		menu.setMenuSeq(seq);
		menu.setVisitility(ATTFrameWork.LOGIC_TRUE.equals(visitility)?ATTFrameWork.LOGIC_TRUE:ATTFrameWork.LOGIN_FALSE);
		updateObject(menu);
	}
	public List<MenuInfo> queryRootMenu(){
		return roleDao.queryRootMenu();
	}
	public List<AuthInfo> queryRootAuth(){
		return roleDao.queryRootAuth();
	}
	public List<RoleInfo> queryRoleInfo(){
		return roleDao.queryRoleInfo();
	}
	public List<MenuInfo> queryMenuInfoByRoleId(String visitility,Integer roleId){
		return roleDao.queryMenuInfoByRoleId(visitility,roleId);
	}
	
	public List<AuthInfo> queryAuthInfoByRoleId(Integer roleId){
		return roleDao.queryAuthInfoByRoleId(roleId);
	}
	public void saveForSetMenuByRole(Integer roleId,Integer[] ids){
	    roleDao.deleteRoleMenuByRoleId(roleId);
	    saveRoleMenuByMenuIds(ids, roleId);
	    FrameCache.cache=false;
	}
	public void saveRoleMenuByMenuIds(Integer[] ids,Integer roleId){
		List<RoleMenu> list = new ArrayList<RoleMenu>();
		for(Integer id:ids){
			RoleMenu rm = new RoleMenu();
			rm.setMenuId(id);
			rm.setRoleId(roleId);
			list.add(rm);
		}
		savebatchObject(list);
	}
	public void saveForSetAuthByRole(Integer roleId,Integer[] ids){
	    roleDao.deleteRoleAuthByRoleId(roleId);
	    saveRoleAuthByMenuIds(ids, roleId);
	    FrameCache.cache=false;
	}
	public void saveRoleAuthByMenuIds(Integer[] ids,Integer roleId){
		List<RoleAuth> list = new ArrayList<RoleAuth>();
		for(Integer id:ids){
			RoleAuth rm = new RoleAuth();
			rm.setAuthId(id);
			rm.setRoleId(roleId);
			list.add(rm);
		}
		savebatchObject(list);
	}
	public void updateConstant(String display,String contsName,Integer sort,String val,Constant conts){
		if(conts!=null){
			conts.setContsName(contsName);
			conts.setSort(sort);
			conts.setVal(val);
			conts.setDisplay(StringUtil.equals(ATTFrameWork.LOGIC_TRUE, display)?ATTFrameWork.LOGIC_TRUE:ATTFrameWork.LOGIN_FALSE);
            updateObject(conts);
		}
	}
	
	public void saveConstant(String display,String pcode,String contsName,Integer sort,String val){
		String code = createPkcodeBycode(pcode);
		if(code!=null){
			Constant conts = new Constant();
			conts.setCode(code);
			conts.setPcode(pcode);
			conts.setContsName(contsName);
			conts.setSort(sort);
			conts.setVal(val);
			conts.setLev(code.length()/3);
			conts.setDisplay(StringUtil.equals(ATTFrameWork.LOGIC_TRUE, display)?ATTFrameWork.LOGIC_TRUE:ATTFrameWork.LOGIN_FALSE);
			saveObject(conts);
		}
	}
	public String createPkcodeBycode(String pcode){
		if(!StringUtil.isEmpty(pcode)){
			Constant cate=roleDao.getMaxCode(pcode);
			if(cate==null){
				return StringUtil.equals(pcode, ATTFrameWork.PCODE)?"001":pcode+"001";
			}else{
				return createCodeByMaxCode(cate.getCode(),cate.getLev());
			}
		}
		return null;
	}
	 private String createCodeByMaxCode(String maxCode,Integer len){
	    	Long code = Long.parseLong(maxCode);
	    	code++;
	    	return fixedZero(code,len);
	    }
    private static String fixedZero(Long code,Integer len){
    	String s = code.toString();
        while(s.length()<len*3){
        	s="0"+s;
        }
        return s;
    }
    public void searchConstants(String name,String pcode,String display,Page page){
		roleDao.searchConstants(name,pcode, display, page);
	}
}
